package lambda;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Comparator;
import java.util.List;
import java.util.function.BinaryOperator;

/**
 * @author Summerday
 */
public class LambdaTest {

    public static void main(String[] args) {
        // java8 之前
        Runnable taskBeforeJava8 = new Runnable() {
            @Override
            public void run() {
                System.out.println("Java8 之前的写法, 传入匿名类");
            }
        };

        Runnable t = new Task();

        // java8 之后
        Runnable taskAfterJava8 = () -> System.out.println("Java8 之后的写法,lambda表达式");

        List<Integer> list = new ArrayList<>(Arrays.asList(1, 2, 3, 4, 5));

        // java8 之前
        list.sort(new Comparator<Integer>() {
            @Override
            public int compare(Integer o1, Integer o2) {
                return o1 - o2;
            }
        });

        // java8 之后
        list.sort((o1, o2) -> o2 - o1);

        // java8 之前
        for (Integer integer : list) {
            System.out.println(integer);
        }

        // java8 之后
        list.forEach(n -> System.out.println(n));
        // java8 之后方法引用
        list.forEach(System.out::println);

        BinaryOperator<Integer> sumFunction = (x, y) -> x + y;
        int sum = sumFunction.apply(1, 3);
        System.out.println(sum);



    }

}

class Task implements Runnable{

    @Override
    public void run() {
        System.out.println("Java8 之前 实现Runnable接口中的run方法");
    }
}
